<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button>fetch ajax请求</button>

    <script>
      let btn = document.querySelector('button');
      btn.onclick = function () {
        // get请求
        // fetch(`http://localhost:8081/server/3?name=tom&age=12`)
        fetch(`http://localhost:8081/server/3?name=tom&age=12`, {
          method: 'POST',
          headers: {
            // 'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Type': 'application/json',
          },
          // body: 'a=1&b=2'
          body: JSON.stringify({a: 1, b: 2})
        })

          .then(response => {
            return response.json()
          })
          .then(data => {
            console.log(data)
          })
      };
    </script>
  </body>
</html>
